<?php

use common\helpers\Html;
use yii\grid\GridView;
use yii\db\Expression;
use kartik\daterange\DateRangePicker;
$this->title = '签到签退';
$this->params['breadcrumbs'][] = $this->title;

?>
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title"><?= Html::encode($this->title) ?></h3>
                <div class="box-tools">
                    <?= Html::create(['create'], '创建', [
                        'data-toggle' => 'modal',
                        'data-target' => '#ajaxModalLg',
                    ]); ?>
                    <?= Html::create(['export', 'member.realname' => $search['SearchModel']['member.realname'], 'type' => $search['SearchModel']['type'],'attendClasses.title' => $search['SearchModel']['attendClasses.title'],'created_at' => $search['SearchModel']['created_at'], 'state' => $search['SearchModel']['state']], '导出',['target'=>'_blank']); ?>
                </div>
            </div>
            <div class="box-body table-responsive">
                <?= GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' => $searchModel,
                    'tableOptions' => [
                        'class' => 'table table-hover rf-table',
                        'fixedNumber' => 2,
                        'fixedRightNumber' => 1,
                    ],
                    'columns' => [
                        [
                            'label' => '打卡图',
                            'filter' => false, //不显示搜索框
                            'value' => function ($model) {
                                if (!empty($model->picture)) {
                                    return \common\helpers\ImageHelper::fancyBox($model->picture);
                                }
                            },
                            'format' => 'raw',
                            'headerOptions' => ['class' => 'col-md-1'],
                        ],
                        [
                            'attribute' => 'member.realname',
                            'filter' => Html::activeTextInput($searchModel, 'member.realname', [
                                    'class' => 'form-control'
                                ]
                            ),
                            'headerOptions' => ['class' => 'col-md-1'],
                        ],
                        [
                            'attribute' => 'type',
                            'value' => function ($model) {
                                return \addons\Attend\common\enums\CheckEnum::getValue($model->type);
                            },
                            'filter' => Html::activeDropDownList($searchModel, 'type', \addons\Attend\common\enums\CheckEnum::getMap(), [
                                    'prompt' => '全部',
                                    'class' => 'form-control'
                                ]
                            )
                        ],
                        [
                            'attribute' => 'attendClasses.title',
                            'filter' => Html::activeTextInput($searchModel, 'attendClasses.title', [
                                    'class' => 'form-control'
                                ]
                            ),
                            'headerOptions' => ['class' => 'col-md-1'],
                        ],

                        [
                            'attribute' => 'created_at',
                            'filter' => DateRangePicker::widget([
                                'name' => 'SearchModel[created_at]',
                                'value' => Yii::$app->request->get('SearchModel')['created_at'],
                                'convertFormat' => true,
                                'pluginOptions' => [
                                    'locale' => [
                                        'format' => 'Y-m-d',
                                        'separator' => '/',
                                    ],
                                ]
                            ]),
                            'value'=>function($model){
                                return date("Y-m-d H:i:s",$model->created_at);
                            },
                        ],
                        [
                            'attribute' => 'state',
                            'value' => function ($model) {
                                return \addons\Attend\common\enums\CheckStateEnum::getValue($model->state);
                            },
                            'filter' => Html::activeDropDownList($searchModel, 'state', \addons\Attend\common\enums\CheckStateEnum::getMap(), [
                                    'prompt' => '全部',
                                    'class' => 'form-control'
                                ]
                            ),
                        ],
                        [
                            'label' => '备注',
                            'value' => function ($model) {
                                if ($model->type == \addons\Attend\common\enums\CheckEnum::SignIn) {
                                    $time = $timediff = $model->created_at - strtotime(date("Y-m-d", time()) . ' ' . $model->begin_time);
                                    $remain = abs($timediff) % 86400;
                                    $hours = intval($remain / 3600);
                                    $remain = $remain % 3600;
                                    $mins = intval($remain / 60);
                                    if ($time > 0)
                                        return '迟到' . $hours . '小时' . $mins . '分钟上班';
                                    if ($time < 0)
                                        return '提前' . $hours . '小时' . $mins . '分钟上班';
                                } else if ($model->type == \addons\Attend\common\enums\CheckEnum::SignOut) {
                                    $time = $timediff = strtotime(date("Y-m-d", time()) . ' ' . $model->end_time) - $model->created_at;
                                    $remain = abs($timediff) % 86400;
                                    $hours = intval($remain / 3600);
                                    $remain = $remain % 3600;
                                    $mins = intval($remain / 60);
                                    if ($time > 0)
                                        return '早退' . $hours . '小时' . $mins . '分钟下班';
                                    if ($time < 0)
                                        return '推迟' . $hours . '小时' . $mins . '分钟下班';
                                }
                            },

                        ],

                        [
                            'class' => 'yii\grid\ActionColumn',
                            'header' => '操作',
                            'headerOptions' => ['width' => '250'],
                            'template' => '{edit} {delete}',
                            'buttons' => [
                                'edit' => function ($url, $model, $key) {
                                    return Html::edit(['ajax-edit', 'id' => $model->id], '编辑', [
                                        'data-toggle' => 'modal',
                                        'data-target' => '#ajaxModalLg',
                                    ]);
                                },
                                'status' => function ($url, $model, $key) {
                                    return Html::status($model->status);
                                },
                                'delete' => function ($url, $model, $key) {
                                    return Html::delete(['delete', 'id' => $model->id]);
                                },
                            ]
                        ]
                    ]
                ]); ?>
            </div>
        </div>
    </div>
</div>
